﻿using System.Drawing;
using System.Runtime.Versioning;
using System.Windows.Forms;
using Microsoft.Win32;

namespace zykControl
{
    [SupportedOSPlatform("windows")]
    public class ZykListView : ListView
    {

        /// <summary>
        /// 在ListView中插入一行,并返回此行的ListViewItem,并且已经为它的所有子项赋空字符串
        /// </summary>
        /// <param name="theText"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public ListViewItem ItemAdd(string theText)
        {
            var dItem = Items.Add(theText);
            for (int xx = 0, loopTo = Columns.Count - 1; xx <= loopTo; xx++)
                dItem.SubItems.Add("");
            return dItem;
        }

        /// <summary>
        /// 在索引为theColumnIndex的列中查找并返回第一个与theText匹配的ListViewItem,没有找到则返回Nothing
        /// </summary>
        /// <param name="theText"></param>
        /// <param name="theColumnIndex"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public ListViewItem? FindFirst(string theText, int theColumnIndex)
        {
            for (int xx = 0; xx < Items.Count; xx++)
            {
                if ((Items[xx].SubItems[theColumnIndex].Text ?? "") == (theText ?? ""))
                    return Items[xx];
            }

            return null;
        }

        /// <summary>
        /// 在索引为theColumn的列中查找并返回第一个与theText匹配的ListViewItem,没有找到则返回Nothing
        /// </summary>
        /// <param name="theText"></param>
        /// <param name="theColumn"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public ListViewItem? FindFirst(string theText, ColumnHeader theColumn)
        {
            return FindFirst(theText, theColumn.Index);
        }

        /// <summary>
        /// 返回被选中的行中的第一行
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public ListViewItem? SelectedItem
        {
            get
            {
                if (SelectedItems.Count == 0)
                {
                    return null;
                }
                else
                {
                    return SelectedItems[0];
                }
            }
        }

        /// <summary>
        /// 向上移动选定行
        /// </summary>
        /// <remarks></remarks>
        public void MoveUpRow()
        {
            if (SelectedItem is null)
                return;
            int dIndex = SelectedItem.Index - 1;
            if (dIndex < 0)
                return;
            Items[dIndex].Selected = true;
            Items[dIndex].Focused = true;
            Items[dIndex].EnsureVisible();
        }

        /// <summary>
        /// 向下移动选定行
        /// </summary>
        /// <remarks></remarks>
        public void MoveDownRow()
        {
            if (SelectedItem is null)
                return;
            int dIndex = SelectedItem.Index + 1;
            if (dIndex > Items.Count - 1)
                return;
            Items[dIndex].Selected = true;
            Items[dIndex].Focused = true;
            Items[dIndex].EnsureVisible();
        }

        /// <summary>
        /// 查找行Tag与theValue相等的第一个行
        /// </summary>
        /// <param name="theValue"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public ListViewItem? FindTag(object theValue)
        {
            for (int xx = 0, loopTo = Items.Count - 1; xx <= loopTo; xx++)
            {
                if (theValue.Equals(Items[xx].Tag) == true)
                    return Items[xx];
            }

            return null;
        }

        /// <summary>
        /// 查找某列子项Tag与theValue相等的第一个行
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="theColumn"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public ListViewItem? FindTag(object theValue, ColumnHeader theColumn)
        {
            return FindTag(theValue, theColumn.Index);
        }

        /// <summary>
        /// 查找某列子项Tag与theValue相等的第一个行
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="theColumnIndex"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public ListViewItem? FindTag(object theValue, int theColumnIndex)
        {
            for (int xx = 0; xx < Items.Count; xx++)
            {
                if (theValue.Equals(Items[xx].SubItems[theColumnIndex].Tag) == true)
                    return Items[xx];
            }

            return null;
        }

        private string mKeyMain = "";

        /// <summary>
        /// 载入列宽度布局
        /// </summary>
        /// <param name="theKeyMain"></param>
        /// <remarks></remarks>
        public void LoadLayout(string theKeyMain)
        {
            mKeyMain = theKeyMain;
            if (DesignMode == true) return;
            if (string.IsNullOrEmpty(mKeyMain)) return;
            if (string.IsNullOrEmpty(FindForm()?.Name)) return;
            if (string.IsNullOrEmpty(Name)) return;

            var dKey = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey(mKeyMain).CreateSubKey(FindForm()!.Name).CreateSubKey(Name);
            for (int xx = 0, loopTo = Columns.Count - 1; xx <= loopTo; xx++)
            {
                var aa = dKey.GetValue(xx.ToString())?.ToString();
                if (string.IsNullOrEmpty(aa)) continue;
                if (!int.TryParse(aa, out var intValue)) continue;

                Columns[xx].Width = intValue;
            }

            dKey.Close();
        }

        /// <summary>
        /// 保存列宽度布局
        /// </summary>
        /// <remarks></remarks>
        public void SaveLayout()
        {
            if (DesignMode == true) return;
            if (string.IsNullOrEmpty(mKeyMain)) return;
            if (string.IsNullOrEmpty(FindForm()?.Name)) return;
            if (string.IsNullOrEmpty(Name)) return;

            var dKey = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey(mKeyMain).CreateSubKey(FindForm()!.Name).CreateSubKey(Name);
            for (int xx = 0, loopTo = Columns.Count - 1; xx <= loopTo; xx++)
                dKey.SetValue(xx.ToString(), Columns[xx].Width.ToString());
            dKey.Close();
        }

        /// <summary>
        /// 选中一行,指定行的索引必须是存在的
        /// </summary>
        /// <param name="RowIndex"></param>
        /// <remarks></remarks>
        public void DoSelectItem(int RowIndex)
        {
            Items[RowIndex].Selected = true;
            Items[RowIndex].Focused = true;
            Items[RowIndex].EnsureVisible();
        }

        /// <summary>
        /// 选中一行
        /// </summary>
        /// <param name="RowItem"></param>
        public void DoSelectItem(ListViewItem RowItem)
        {
            DoSelectItem(RowItem.Index);
        }

        /// <summary>
        /// 删除一行,并选中指定的一行
        /// </summary>
        /// <remarks></remarks>
        public void DoRemoveItem(int RowIndex, bool SelectThisIndex = false)
        {
            int dIndex = RowIndex;
            if (SelectThisIndex == false)
                dIndex -= 1;
            Items.RemoveAt(RowIndex);
            if (Items.Count == 0)
                return;
            if (dIndex < 0)
                dIndex = 0;
            if (dIndex > Items.Count - 1)
                dIndex = Items.Count - 1;
            if (dIndex < 0)
                return;
            DoSelectItem(dIndex);
        }

        public void DoRemoveItem(ListViewItem RowItem, bool SelectThisIndex = false)
        {
            DoRemoveItem(RowItem.Index, SelectThisIndex);
        }

        //public void DrawColumnHeaderSub(DrawListViewColumnHeaderEventArgs e)
        //{
        //    if (e.Header == null) return;
        //    e.DrawBackground();
        //    var dFormat = new StringFormat(StringFormat.GenericTypographic) { Alignment = StringAlignment.Center };
        //    e.Graphics.DrawString(e.Header.Text, e.Header.ListView!.FindForm()!.Font, Brushes.Black, new Rectangle(e.Bounds.X + 3, e.Bounds.Y + 3, e.Bounds.Width - 6, e.Bounds.Height - 6), dFormat);
        //}

        //public void DrawSubItemSub(DrawListViewSubItemEventArgs e, Color theForeColor)
        //{
        //    if (e.DrawDefault == true || e.Item == null || e.Header == null || e.Header.ListView == null || e.SubItem == null) return;

        //    if (e.Item.Selected)
        //    {
        //        e.Graphics.FillRectangle(Brushes.WhiteSmoke, e.Bounds);
        //    }
        //    else
        //    {
        //        e.Graphics.FillRectangle(new SolidBrush(e.Item.BackColor), e.Bounds);
        //    }

        //    if (e.ItemIndex == 0 && e.Header.Index == 0)
        //    {
        //        e.Graphics.DrawRectangle(Pens.White, e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);
        //    }
        //    else if (e.ItemIndex == 0)
        //    {
        //        e.Graphics.DrawLines(Pens.White, new Point[] { new(e.Bounds.X, e.Bounds.Y), new(e.Bounds.X + e.Bounds.Width - 1, e.Bounds.Y), new(e.Bounds.X + e.Bounds.Width - 1, e.Bounds.Y + e.Bounds.Height - 1), new(e.Bounds.X, e.Bounds.Y + e.Bounds.Height - 1) });
        //    }
        //    else if (e.Header.Index == 0)
        //    {
        //        e.Graphics.DrawLines(Pens.White, new Point[] { new(e.Bounds.X, e.Bounds.Y), new(e.Bounds.X, e.Bounds.Y + e.Bounds.Height - 1), new(e.Bounds.X + e.Bounds.Width - 1, e.Bounds.Y + e.Bounds.Height - 1), new(e.Bounds.X + e.Bounds.Width - 1, e.Bounds.Y) });
        //    }
        //    else
        //    {
        //        e.Graphics.DrawLines(Pens.White, new Point[] { new(e.Bounds.X, e.Bounds.Y + e.Bounds.Height - 1), new(e.Bounds.X + e.Bounds.Width - 1, e.Bounds.Y + e.Bounds.Height - 1), new(e.Bounds.X + e.Bounds.Width - 1, e.Bounds.Y) });
        //    }

        //    if (e.Item.Selected)
        //    {
        //        e.Graphics.DrawLine(Pens.Blue, e.Bounds.Left, e.Bounds.Top + e.Bounds.Height - 1, e.Bounds.Left + e.Bounds.Width - 1, e.Bounds.Top + e.Bounds.Height - 1);
        //        e.Graphics.DrawLine(Pens.Blue, e.Bounds.Left, e.Bounds.Top, e.Bounds.Left + e.Bounds.Width - 1, e.Bounds.Top);
        //        if (e.Header.Index == 0)
        //            e.Graphics.DrawLine(Pens.Blue, e.Bounds.Left, e.Bounds.Top, e.Bounds.Left, e.Bounds.Top + e.Bounds.Height - 1);
        //        if (e.Header.Index == e.Header.ListView.Columns.Count - 1)
        //            e.Graphics.DrawLine(Pens.Blue, e.Bounds.X + e.Bounds.Width - 1, e.Bounds.Top, e.Bounds.X + e.Bounds.Width - 1, e.Bounds.Top + e.Bounds.Height - 1);
        //    }
        //    // e.DrawFocusRectangle(e.Item.Bounds)
        //    var dFormat = new StringFormat(StringFormat.GenericTypographic);
        //    switch (e.Header.TextAlign)
        //    {
        //        case HorizontalAlignment.Center:
        //            {
        //                dFormat.Alignment = StringAlignment.Center;
        //                break;
        //            }

        //        case HorizontalAlignment.Left:
        //            {
        //                dFormat.Alignment = StringAlignment.Near;
        //                break;
        //            }

        //        case HorizontalAlignment.Right:
        //            {
        //                dFormat.Alignment = StringAlignment.Far;
        //                break;
        //            }
        //    }

        //    dFormat.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;
        //    dFormat.Trimming = StringTrimming.EllipsisCharacter;
        //    dFormat.FormatFlags = StringFormatFlags.NoWrap;
        //    e.Graphics.DrawString(e.SubItem.Text, e.Header.ListView.Font, new SolidBrush(theForeColor), new Rectangle(e.Bounds.X + 5, e.Bounds.Y + 5, e.Bounds.Width - 10, e.Bounds.Height - 7), dFormat);
        //}
    }
}